export default {
  state: () => ({
        // 服务器数据
        banners: [],
        recommends: []
  }),
  mutations: {
    changeBanner(state, banner){
      state.banners = banner
    },
    changeRecommends(state, recommend){
      state.recommends = recommend
    }
  },
  actions: {
    fetchHomeMultiDataAction(context) {
      // 1. 返回promise对象,给promise对象添加一个then方法
      // 返回整个 Promise 链
    return fetch('http://123.207.32.32:8000/home/multidata')
      .then(res => res.json())
      .then(data => {
        console.log('API Response:', data);
        context.commit('changeBanner', data.data.banner.list);
        context.commit('changeRecommends', data.data.recommend.list);
        return new Promise((resolve, reject) => {
          resolve("aaaa"); // 手动返回固定字符串
        });
      })
      .catch(error => {
        console.error('API 请求失败:', error);
        throw error;
      });

      // 2. promise链式调用
      // fetch('http://123.207.32.32:8000/home/data').then(res => {
      //   return res.json()  
      // }).then(data => {
      //   console.log(data)
      // })

    },
    // 3. async await
    async fetchHomeMultiDataAction2(context) { // 异步函数自动返回promise对象
      const res = await fetch('http://123.207.32.32:8000/home/multidata')
      const data = await res.json()
      console.log(data)
    },

    fetchHomeMultiDataAction3(context) { // 异步函数自动返回promise对象
      return new Promise(async (resolve, reject) => {
        const res = await fetch('http://123.207.32.32:8000/home/multidata')
        const data = await res.json()
        console.log(data)       
      })
    }
  }
}